package magics
{
	import core.actor.core.ActorView;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	import loader.LoadProxy;
	import loader.core.LoadType;

	public class Magic implements ITickable
	{
		
		public function Magic()
		{
			_magics = [];
		}
		
		public function initAni(aniUrl:String):void
		{
			var loadAni:LoadProxy;//角色动作加载代理
			loadAni = new LoadProxy(LoadType.LOAD_MAGIC_ANI);
			loadAni.load(aniUrl, handleAniResult);
		
		}
		
		private function handleAniResult(data:Object):void
		{
			_magicAni = data.ani;
			_aniLength = _magicAni.length;	
		}
		
		//初始化魔法层,一个magic只支持一层
		public function init(magicLayer:Sprite):void
		{
			_magicLayer = magicLayer;
		}

		public function get tickType():int
		{
			return 0;
		}
		
		public function update(step:int):void
		{
			if(_magicAni == null) return;
			updateMagicAni();
			_curFrame++;
			if(_curFrame>_aniLength-1)
			{
				_curFrame = 0;
				TickManager.instance.removeObject(this);
				//删除魔法效果
				//clearMagic();
			}
				
		}
		
		public function setProperty(propertyName:String,p:Number):void
		{
			switch(propertyName)
			{
				case "x":
					_x = p;
					break;
				case "y":
					_y = p;
					break;
				case "rotation":
					_rotation = p;
					break;
				case "alpha":
					_alpha = p;
					break;
				case "scaleX":
					_scaleX = p;
					break;
				case "scaleY":
					_scaleY = p;
					break;
			}
		}
		
		public function play():void
		{
			//clearMagic();
			TickManager.instance.addObject(this);
		}
		
		
		private function initMagic(magicPartName:String,cacheType:String = "mc",cacheSize:Rectangle = null,preInit:Boolean = false):MagicSprite
		{
			var m:MagicSprite;
			m = MagicSpriteCache.instance.getSprite();
			m.initMagic(magicPartName,cacheType,cacheSize,preInit);
			_magics[magicPartName] = m;
			return m;
		}
		
		private function clearMagic():void
		{
			for each(var magic:MagicSprite in _magics)
			{
				if(magic.isAdded)
					magic.removeMagic(_magicLayer);
			}
		}
		
		private function updateMagicAni():void
		{
			var ani:Array = _magicAni[_curFrame] as Array;
			if(ani == null) return;
			for(var i:int = 0;i<ani.length;i++)
			{
				var data:MagicData = ani[i] as MagicData;
				var path:String = "../testRes/"+data.name+".swf";//临时使用，应移到pathHelper中
				var target:MagicSprite  = _magics[path] as MagicSprite;
				if(target == null)
				{
					if(data.cacheType == "bmd")
						target = initMagic(path,"bmd",new Rectangle(0,0,data.cacheWidth,data.cacheHeight),false);
					else
						target = initMagic(path);
				}
				if(!target.isAdded)
				{
					target.addMagic(_magicLayer);
					target.play();
				}
				if(target.displayObject !=null)
				{
					target.setProperty("x",data.x+_x);
					target.setProperty("y",data.y+_y);
					target.setProperty("alpha",data.alpha*_alpha);
					target.setProperty("rotation",data.rotation+_rotation);
					target.setProperty("scaleX",data.scaleX*_scaleX);
					target.setProperty("scaleY",data.scaleY*_scaleY);
					target.setProperty("blendMode",data.blendMode);
				}
				
				/*if(target.x!=data.x)
					target.x = data.x;
				if(target.y!=data.y)
					target.y = data.y;
				if(target.alpha!=data.alpha)
					target.alpha = data.alpha;
				if(target.rotation!=data.rotation)
					target.rotation = data.rotation;
				if(target.scaleX!=data.scaleX)
					target.scaleX = data.scaleX;
				if(target.scaleY!=data.scaleY)
					target.scaleY = data.scaleY;*/
			}
			//使用魔法动画Ani文件播放动画
			
		}
		
		
		private var _x:Number = 0;
		private var _y:Number = 0;
		private var _alpha:Number = 1;
		private var _rotation:Number = 0;
		private var _scaleX:Number = 1;
		private var _scaleY:Number = 1;
		
		private var _aniLength:int = 0;
		private var _curFrame:int = 0;
		private var _magics:Array;
		private var _magicAni:Array;
		private var _magicLayer:Sprite;
	}
}